﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SCOSub.FFMS2;

namespace SCOSub
{
    public partial class LyricViewr : Form
    {
        public static int LastSelectedThumbID = 0;
        public FrameThumb[] FrameThumbArray = null;
        FFMS2VideoProvider ff2;
        int pageCount;
        public LyricViewr()
        {
            
            InitializeComponent();
            FrameThumbArray = new FrameThumb[24]; 
            FrameThumbArray[0] = frameThumb1;
            FrameThumbArray[1] = frameThumb2;
            FrameThumbArray[2] = frameThumb3;
            FrameThumbArray[3] = frameThumb4;
            FrameThumbArray[4] = frameThumb5;
            FrameThumbArray[5] = frameThumb6;
            FrameThumbArray[6] = frameThumb7;
            FrameThumbArray[7] = frameThumb8;
            FrameThumbArray[8] = frameThumb9;
            FrameThumbArray[9] = frameThumb10;
            FrameThumbArray[10] = frameThumb11;
            FrameThumbArray[11] = frameThumb12;
            FrameThumbArray[12] = frameThumb13;
            FrameThumbArray[13] = frameThumb14;
            FrameThumbArray[14] = frameThumb15;
            FrameThumbArray[15] = frameThumb16;
            FrameThumbArray[16] = frameThumb17;
            FrameThumbArray[17] = frameThumb18;
            FrameThumbArray[18] = frameThumb19;
            FrameThumbArray[19] = frameThumb20;
            FrameThumbArray[20] = frameThumb21;
            FrameThumbArray[21] = frameThumb22;
            FrameThumbArray[22] = frameThumb23;
            FrameThumbArray[23] = frameThumb24;
            for (int i = 0; i < 24; i++)
            {
                FrameThumbArray[i].ThumbID = i;
                FrameThumbArray[i].ftarray = FrameThumbArray;
            }
            
        }
        public void OpenFile(string filename)
        {
            try
            {
                ff2 = new FFMS2VideoProvider(filename, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                this.Dispose(true);
                throw;
            }
            pageCount = ff2.Videoproperties.NumFrames / 24 + (ff2.Videoproperties.NumFrames % 24 == 0 ? 0 : 1);
            vScrollBar1.Minimum = 1;
            vScrollBar1.Maximum = pageCount;
            vScrollBar1.Value = 1;
            for (int i = 0; i < 24; i++)
            {
                FrameThumbArray[i].FrameBMP = new Bitmap(100, 100);
            }
            getpageBMP(0, 24);
            panel2.Refresh();
        }
        private void getpageBMP(int startframe, int length)
        {
            int p = 0;
            for (int i = startframe; i < startframe + length; p++, i++)
            {
                BMPOpera.Resize(ff2.GetFrameBitmap(i), FrameThumbArray[p].FrameBMP, new Size(100, 100));
                FrameThumbArray[p].FrameNumber = i;
            }
        }

        private void LyricViewr_Load(object sender, EventArgs e)
        {

        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            int start = (vScrollBar1.Value - 1) * 24;
            getpageBMP(start, 24);
            panel2.Refresh();
        }
    }
}
